package com.rockcent.message;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

/**
 * Created by zhangjh on 2017/5/23.
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    /**
     * 定义消息代理，设置消息连接请求的各种规范信息。
     * enableSimpleBroker：客户端接收服务端消息的地址的前缀信息
     * setApplicationDestinationPrefixes：通知前缀
     *
     * @param config
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/msg","/user");
        config.setUserDestinationPrefix("/user/");
//        config.setApplicationDestinationPrefixes("/app");api前缀，客户端给服务端发消息的地址的前缀
    }


    /**
     * 添加一个服务端点，来接收客户端的连接
     *
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        //客户端通过这个端点来进行连接
        registry.addEndpoint("/websocket/init").setAllowedOrigins("*").withSockJS();
    }

}